<?php
class Utils {
	
	//dopsat obranu proti SQL injekci...
	//bo jedine z formulare muze prijit bordel..
	
	static function requestGet($value){
		return $_GET[$value];
	}
	
	static function requestPost($value){
		return $_POST[$value];
	}
	
	/**
	 * Vraci retezec jmen parametru a jejich hodnot zacinajici ?
	 * pr. ?cat=8&subcat=4&page=2
	 */
	static function rewriteParameters(){
		
		$url = "?";
		
		if($_GET){
					
			foreach ( $_GET as $key => $value ) {
       			$url = $url.$key."=".$value."&";
		 	}
			$new = rtrim($url,'&');
		} else {
			return "";
		}
	
		return $new;
	}
	
	static function rewriteParametersWithout($param){
		
		$url = "?";
		
		if($_GET){
					
			foreach ( $_GET as $key => $value ) {
       			if($key != $param){
       				$url = $url.$key."=".$value."&";
       			}
		 	}
			$new = rtrim($url,'&');
		} else {
			return "";
		}
	
		return $new;
	}
	
	static function sessionSet($key, $value){
		$_SESSION[$key] = $value;
	}
	
	static function sessionGet($key){
		return $_SESSION[$key];
	}
	
	static function sessionRemove($key){
		unset($_SESSION[$key]);
	}
	
	/**
	 * Vrati pole obsahujici jen nazvy parametru pri pozadavku
	 */
	static function getRequestParameterNames(){
		
		$array = array();
		
		if($_POST){
			foreach ( $_POST as $key => $value ) {
       			$array[] = $key;
			}
		}else {
			foreach ( $_GET as $key => $value ) {
       			$array[] = $key;
			}
		}
	
		return $array;
	}
	
	/**
	 * Vrati pole obsahujici jen hodnoty parametru pri pozadavku
	 */
	static function getRequestParameterValues(){
		
		$array = array();
		
		if($_POST){
			foreach ( $_POST as $key => $value ) {
       			$array[] = $value;
			}
		}else {
			foreach ( $_GET as $key => $value ) {
       			$array[] = $value;
			}
		}
	
		return $array;
	}
	
	static function inputHidden($name, $value){
		$hiden = "<input type='hidden' name='$name' value='$value'>";
		return self::out($hiden);
	}
	
	static function form($action, $method, $inside){
		$form = "<form action='$action' method='$method'>" .
				"$inside" .
				"</form>";
				
		return self::out($form);
	}
	
	static function out($string){
		echo $string;
	}
	
	static function sendRedirect($where){
		header("Location:$where");
		exit;
	}
	
	static function sendError($where, $msg){
		self::sessionSet("ERROR",$msg);
		self::sendRedirect($where);
	}
	
}
